﻿using Mahanet.Abyss.PathCalculation;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace motion
{
	public class PointsLayer : BaseLayer
	{
		protected class DrawData
		{
			#region C'tor

			public DrawData(string datasetName, IEnumerable<Coordinate2D> points, Color color, int size)
			{
				this.DatasetName = datasetName;
				this.PointLocations = points;
				this.DrawColor = color;
				this.DrawWidth = size;
			}

			#endregion

			#region Public Properties

			public string DatasetName { get; private set; }

			public IEnumerable<Coordinate2D> PointLocations { get; private set; }

			public Color DrawColor { get; private set; }

			public int DrawWidth { get; private set; }

			#endregion
		}

		#region Fields

		private List<DrawData> m_PointsToDraw;
		private readonly Color m_DefaultColor;

		#endregion

		#region C'tor

		public PointsLayer(Control parentController, Color defaultColor)
			: base(parentController)
		{
			m_PointsToDraw = new List<DrawData>();
			m_DefaultColor = defaultColor;
		}

		#endregion

		#region Public Methods

		public void AddPointsToDraw(string datasetName, IEnumerable<Coordinate2D> points, Color color, int size)
		{
			if (color == null)
			{
				color = Color.Blue;
			}

			//this.m_PointsToDraw.Add(new DrawData(datasetName, points, color, size));
		}

		public void AddPointsToDraw(string datasetName, IEnumerable<Coordinate2D> points)
		{
			//AddPointsToDraw(datasetName, points, m_DefaultColor, 5);
		}

		public void AddPointsToDraw(string datasetName, Coordinate2D point, Color color, int size)
		{
			//AddPointsToDraw(datasetName, new List<Coordinate2D>() { point }, color, size);
		}

		public void AddPointsToDraw(string datasetName, Coordinate2D point)
		{
			//AddPointsToDraw(datasetName, new List<Coordinate2D>() { point });
		}

		public void ClearDatasetPoints(string datasetName)
		{
			m_PointsToDraw.RemoveAll(drawData => drawData.DatasetName == datasetName);
		}

		#endregion

		#region Protected Methods

		protected override void OnLayerPaint(PaintEventArgs pe)
		{
			Graphics grapics = pe.Graphics;

			foreach (DrawData drawData in m_PointsToDraw)
			{
        using (Brush drawBrush = new SolidBrush(drawData.DrawColor))
        {
          foreach (var point in drawData.PointLocations)
          {
            if (point.X < 0 || point.X > ParentController.Width ||
              point.Y < 0 || point.Y > ParentController.Height)
            {
              continue;
            }
            grapics.FillEllipse(drawBrush,
                       (int)point.X,
                       ParentController.Height - drawData.DrawWidth - (int)point.Y,
                       drawData.DrawWidth,
                       drawData.DrawWidth);
          }
        }
			}
		}

		#endregion
	}
}
